package see.battle.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import see.battle.Const;
import see.battle.message.BulletMessage;
import see.battle.message.Message;
import see.battle.message.OpponentFocusMessage;
import see.battle.message.StartGameMessage;

import junit.framework.TestCase;

public class MessageTestCase extends TestCase {

  Message _m1;
  Message _m2;
  Message _m3;
  
	protected void setUp() throws Exception {
		super.setUp();
		ByteArrayOutputStream pout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(pout);
    StartGameMessage m1 = new StartGameMessage(100l, 42); 
    BulletMessage m2 = new BulletMessage(120l, (float)0.5, 10l);
    OpponentFocusMessage m3 = new OpponentFocusMessage((float)0.2);
    out.writeObject(m1);
    out.writeObject(m2);
    out.writeObject(m3);
    ByteArrayInputStream pin = new ByteArrayInputStream(pout.toByteArray());
    ObjectInputStream in = new ObjectInputStream(pin);
    _m1 = (Message)in.readObject();
    _m2 = (Message)in.readObject();
    _m3 = (Message)in.readObject();
	}	
	
	public void testMessages() {
    assertEquals(_m1.type(), Const.startGameMessageType);
    assertEquals(((StartGameMessage)_m1).get_time(), 100l);
    assertEquals(((StartGameMessage)_m1).get_seed(), 42);
    assertEquals(_m2.type(), Const.bulletMessageType);
    assertEquals(((BulletMessage)_m2).get_time(), 120l);
    assertEquals(((BulletMessage)_m2).get_position(), (float)0.5);
    assertEquals(((BulletMessage)_m2).get_additionalShipDelay(), 10l);
    assertEquals(_m3.type(), Const.opponentFocusMessageType);
    assertEquals(((OpponentFocusMessage)_m3).get_focus(), (float)0.2);
	}
	
}

